package com.alander.nexusgo.itinerary.service.impl;

import com.alander.nexusgo.common.model.dto.ItineraryCollaboratorListDTO;
import com.alander.nexusgo.common.model.vo.ItineraryCollaboratorVO;
import com.alander.nexusgo.itinerary.mapper.ItineraryCollaboratorMapper;
import com.alander.nexusgo.itinerary.model.entity.ItineraryCollaborator;
import com.alander.nexusgo.itinerary.service.ItineraryCollaboratorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class ItineraryCollaboratorServiceImpl extends ServiceImpl<ItineraryCollaboratorMapper, ItineraryCollaborator>
    implements ItineraryCollaboratorService {

    @Override
    public ItineraryCollaboratorVO createCollaborator(ItineraryCollaborator itineraryCollaborator, Long userId) {
        // 这里可能需要检查用户是否有权限添加协作者
        this.save(itineraryCollaborator);
        return convertToVO(itineraryCollaborator);
    }

    @Override
    public ItineraryCollaboratorVO updateCollaborator(Long id, ItineraryCollaborator itineraryCollaborator, Long userId) {
        ItineraryCollaborator existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("协作者不存在");
        }

        // 这里可能需要检查用户是否有权限更新该协作者
        itineraryCollaborator.setId(id);
        this.updateById(itineraryCollaborator);
        return convertToVO(this.getById(id));
    }

    @Override
    public boolean deleteCollaborator(Long id, Long userId) {
        ItineraryCollaborator existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("协作者不存在");
        }

        // 这里可能需要检查用户是否有权限删除该协作者
        // 软删除
        LambdaUpdateWrapper<ItineraryCollaborator> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ItineraryCollaborator::getId, id)
                .set(ItineraryCollaborator::getDelFlag, "1");
        return this.update(updateWrapper);
    }

    @Override
    public ItineraryCollaboratorVO getCollaboratorById(Long id) {
        ItineraryCollaborator itineraryCollaborator = this.getById(id);
        return itineraryCollaborator != null ? convertToVO(itineraryCollaborator) : null;
    }

    @Override
    public List<ItineraryCollaboratorVO> listCollaborators(ItineraryCollaboratorListDTO itineraryCollaboratorListDTO) {
        LambdaQueryWrapper<ItineraryCollaborator> queryWrapper = new LambdaQueryWrapper<>();

        if (itineraryCollaboratorListDTO.getItineraryId() != null) {
            queryWrapper.eq(ItineraryCollaborator::getItineraryId, itineraryCollaboratorListDTO.getItineraryId());
        }

        if (itineraryCollaboratorListDTO.getUserId() != null) {
            queryWrapper.eq(ItineraryCollaborator::getUserId, itineraryCollaboratorListDTO.getUserId());
        }

        if (itineraryCollaboratorListDTO.getRole() != null) {
            queryWrapper.eq(ItineraryCollaborator::getRole, itineraryCollaboratorListDTO.getRole());
        }

        if (itineraryCollaboratorListDTO.getInviteStatus() != null) {
            queryWrapper.eq(ItineraryCollaborator::getInviteStatus, itineraryCollaboratorListDTO.getInviteStatus());
        }

        // 只查询未删除的协作者
        queryWrapper.eq(ItineraryCollaborator::getDelFlag, "0");

        queryWrapper.orderByDesc(ItineraryCollaborator::getCreateTime);
        List<ItineraryCollaborator> itineraryCollaboratorList = this.list(queryWrapper);

        return itineraryCollaboratorList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 将ItineraryCollaborator实体转换为ItineraryCollaboratorVO
     * @param itineraryCollaborator ItineraryCollaborator实体
     * @return ItineraryCollaboratorVO对象
     */
    private ItineraryCollaboratorVO convertToVO(ItineraryCollaborator itineraryCollaborator) {
        ItineraryCollaboratorVO itineraryCollaboratorVO = new ItineraryCollaboratorVO();
        BeanUtils.copyProperties(itineraryCollaborator, itineraryCollaboratorVO);
        if (itineraryCollaborator.getJoinedTime() != null) {
            itineraryCollaboratorVO.setJoinedTime(itineraryCollaborator.getJoinedTime().toString());
        }
        if (itineraryCollaborator.getCreateTime() != null) {
            itineraryCollaboratorVO.setCreateTime(itineraryCollaborator.getCreateTime().toString());
        }
        if (itineraryCollaborator.getUpdateTime() != null) {
            itineraryCollaboratorVO.setUpdateTime(itineraryCollaborator.getUpdateTime().toString());
        }
        return itineraryCollaboratorVO;
    }
}